package com.example.afinal;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class SQLMsgActivity extends AppCompatActivity {
//模拟建立一个留言社区，实现与SQLite数据库的连接，用户可以匿名向数据库添加自己对某个mbti的看法评论，使用toast显示上传记录成功否
//通过点击提交存储到数据库中，并通过点击查询看到别人的评论，实现对mbti的更多了解
    private EditText mbti;
    private EditText comment;
    private Button submit;
    Button ibutton;
    private DBHelper mDbHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main6);


        ibutton = (Button)findViewById(R.id.btn2);
        ibutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(SQLMsgActivity.this, ListActivity.class);
                startActivity(intent);
            }
        });

        mDbHelper = new DBHelper(this);

        mbti = findViewById(R.id.mbti);
        comment = findViewById(R.id.comment);
        submit = findViewById(R.id.btn);

        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String mbti1 = mbti.getText().toString();
                String comment1 = comment.getText().toString();

                SQLiteDatabase db = mDbHelper.getWritableDatabase();

                ContentValues values = new ContentValues();
                values.put(DBHelper.COLUMN_MBTI, mbti1);
                values.put(DBHelper.COLUMN_COMMENT, comment1);

                long newRowId = db.insert(DBHelper.TABLE, null, values);
                if (newRowId == -1) {
                    Toast.makeText(SQLMsgActivity.this, "提交失败", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(SQLMsgActivity.this, "提交成功", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}


